"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomModel = void 0;
const Config_1 = require("../../src/config/Config");
const GameConfig_1 = require("../../src/config/GameConfig");
const RoomConfig_1 = require("../../src/config/RoomConfig");
const ServerConfig_1 = require("../../src/config/ServerConfig");
const Logger_1 = require("../../src/engine/utils/Logger");
const RandomUtil_1 = require("../../src/engine/utils/RandomUtil");
const RoomDao_1 = require("../dao/RoomDao");
const GameResult_1 = require("./GameResult");
const RoomGameInfo_1 = require("./RoomGameInfo");
class RoomModel {
    constructor() {
        this.roomType = 0;
        this.creator = 0; //房主用户id
        this.clubid = 0;
        this.num_of_turns = 0;
        this.playerList = [];
        this.game_info = new RoomGameInfo_1.RoomGameInfo();
        this.bankerChair = 0;
        this.current_chair = 0;
        this.gameResult = new GameResult_1.GameResult();
        this.lastCheckAutoReadyTIme = 0;
    }
    static generateRoomId() {
        let room_id = RandomUtil_1.default.nextInt(100000, 999999);
        if (this.roomMap.has(room_id)) {
            return this.generateRoomId();
        }
        return room_id;
    }
    static init() {
        return __awaiter(this, void 0, void 0, function* () {
            let rows = yield RoomDao_1.RoomDao.get_all_room();
            this.roomMap.clear();
            for (let i = 0; i < rows.length; i++) {
                this.initOneRoom(rows[i], true);
            }
            Logger_1.Logger.log("RoomDao.get_all_roo===", this.roomMap);
        });
    }
    static initOneRoom(obj, forceInit = false) {
        let model = {};
        model.serverId = obj["server_id"];
        let needInit = false;
        if (ServerConfig_1.ServerConfig.nowServerConfig.serverType == Config_1.Config.ServerType_List.hall_server) {
            needInit = true;
        }
        else {
            if (model.serverId == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                needInit = true;
            }
        }
        if (needInit || forceInit) {
            model.roomId = obj["room_id"];
            model.name = obj["name"];
            model.creator = obj["creator"];
            model.roomType = obj["roomType"];
            model.createTime = new Date(obj["create_time"]).getTime();
            model.clubid = obj["clubid"];
            model.numOfTurns = obj["num_of_turns"];
            model.gameType = obj["game_type"];
            model.playerList = [];
            model.roomConfigBase = JSON.parse(obj["conf_base"]);
            model.gameInfoBase = JSON.parse(obj["game_info_base"]);
            if (model.gameType == GameConfig_1.GameConfig.Game_Type.Mj) {
                model.roomConfigMj = JSON.parse(obj["config_game"]);
                model.gameInfoMj = JSON.parse(obj["config_game"]);
            }
            model.playerList = JSON.parse(obj["playerList"]);
            this.roomMap.set(model.roomId, model);
        }
        if (RoomModel.getPlayerNum(model) <= 0) {
            this.roomMap.delete(model.roomId);
        }
        // Logger.log("initOneRoom===", model)
        return model;
    }
    static insertRoom(model) {
        return __awaiter(this, void 0, void 0, function* () {
            let entity = yield RoomDao_1.RoomDao.insert_room(model);
            return entity;
        });
    }
    static insertPlayer(player, room) {
        for (let i = 0; i < room.playerList.length; i++) {
            if (room.playerList[i] && room.playerList[i].userid == player.userid) {
                player.chair = i;
                room.playerList[i] = player;
                return true;
            }
        }
        let renshu = room.roomConfigBase.renshu;
        for (let i = 0; i < renshu; i++) {
            if (!room.playerList[i]) {
                player.chair = i;
                room.playerList[i] = player;
                return true;
            }
        }
    }
    leavePlayer(userid) {
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] && this.playerList[i].userid == userid) {
                this.playerList[i] = null;
                return true;
            }
        }
    }
    getPlayerByUserid(userid) {
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] && this.playerList[i].userid == userid) {
                return this.playerList[i];
            }
        }
    }
    getPlayerByChair(chair) {
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] && this.playerList[i].chair == chair) {
                return this.playerList[i];
            }
        }
    }
    hadPlayer(userid) {
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] && this.playerList[i].userid == userid) {
                return true;
            }
        }
    }
    static getPlayerNum(room) {
        let num = 0;
        for (let i = 0; i < room.playerList.length; i++) {
            if (room.playerList[i]) {
                num++;
            }
        }
        return num;
    }
    isAllReady() {
        let readyNum = 0;
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] && this.playerList[i].ready) {
                readyNum++;
            }
        }
        if (readyNum >= this.conf["renshu"]) {
            return true;
        }
    }
    nextChair() {
        this.game_info.current_chair++;
        if (this.game_info.current_chair >= this.playerList.length) {
            this.game_info.current_chair = 0;
        }
        // if (this.game_info.handCardList[this.game_info.current_chair].length <= 0) {
        //     this.nextChair();
        // }
    }
    nextBanker() {
        this.bankerChair++;
        if (this.bankerChair >= this.playerList.length) {
            this.bankerChair = 0;
        }
    }
    nextCurrentChair() {
        this.current_chair++;
        if (this.current_chair >= this.playerList.length) {
            this.current_chair = 0;
        }
    }
    static getRoomListByClubid(clubid) {
        let roomList = [];
        this.roomMap.forEach((model, key) => {
            if (model.clubid == clubid) {
                roomList.push(model);
            }
        });
        return roomList;
    }
    static getRoomListZiyou(level) {
        let roomList = [];
        this.roomMap.forEach((model, key) => {
            if (model.roomType == RoomConfig_1.RoomConfig.Room_Type.Ziyou) {
                if (model.conf["level"] == level) {
                    roomList.push(model);
                }
            }
        });
        return roomList;
    }
    static getRoomByRoomTypeAndLevel(roomType, gameType, level, renshu) {
        let result;
        this.roomMap.forEach((model, key) => {
            if (model.roomType == roomType) {
                if (model.gameType == gameType) {
                    let config = model.conf;
                    if (config.level == level && config.renshu == renshu) {
                        if (RoomModel.getPlayerNum(model) < config.renshu) {
                            result = model;
                            return model;
                        }
                    }
                }
            }
        });
        return result;
    }
    static getRoomByRoomType(roomType, gameType) {
        let result;
        this.roomMap.forEach((model, key) => {
            if (model.roomType == roomType) {
                if (model.gameType == gameType) {
                    let config = model.conf;
                    if (RoomModel.getPlayerNum(model) < config.renshu) {
                        result = model;
                        return model;
                    }
                }
            }
        });
        return result;
    }
    static getRoomOnlineNumByGameType(roomType, gameType, level) {
        let online = 0;
        this.roomMap.forEach((model, key) => {
            if (model.roomType == roomType) {
                if (model.gameType == gameType) {
                    let config = model.conf;
                    if (config.level == level) {
                        online += RoomModel.getPlayerNum(model);
                    }
                }
            }
        });
        return online;
    }
    static getRoomOnlineByRoomType(roomType, level) {
        let online = 0;
        this.roomMap.forEach((model, key) => {
            if (model.roomType == roomType) {
                let config = model.conf;
                if (config.level == level) {
                    online += RoomModel.getPlayerNum(model);
                }
            }
        });
        return online;
    }
    static getRoomOnlineNumByRoomTypeAndGameType(roomType, gameType) {
        let online = 0;
        this.roomMap.forEach((model, key) => {
            if (model.roomType == roomType) {
                if (model.gameType == gameType) {
                    online += RoomModel.getPlayerNum(model);
                }
            }
        });
        return online;
    }
    static isInRoom(userid) {
        // let inRoom:boolean = false
        let roomId = 0;
        this.roomMap.forEach((model, key) => {
            for (let i = 0; i < model.playerList.length; i++) {
                if (model.playerList[i] && model.playerList[i].userid == userid) {
                    roomId = model.room_id;
                    return roomId;
                }
            }
        });
        return roomId;
    }
}
exports.RoomModel = RoomModel;
RoomModel.roomMap = new Map();
